package tech.aistar.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import tech.aistar.model.entity.Cart;
import tech.aistar.model.entity.Collect;
import tech.aistar.model.entity.Product;
import tech.aistar.model.entity.User;
import tech.aistar.model.vo.Result;
import tech.aistar.service.ICollectService;
import tech.aistar.service.IProductService;
import tech.aistar.service.impl.CollectServiceImpl;
import tech.aistar.service.impl.ProductServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(urlPatterns = "/login/collect/*")
public class CollectController extends HttpServlet {
          IProductService productService = new ProductServiceImpl();
          ICollectService collectService = new CollectServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String path = req.getPathInfo();

        switch (path)
        {

            case "/add":
                resp.setContentType("application/json;charset=utf-8");
                PrintWriter out = resp.getWriter();

                String pid = req.getParameter("pid");
                if(pid!=null)
                {
                    Product c = productService.getById(Integer.valueOf(pid));

                    Collect collect = new Collect();

                    collect.setProductId(c.getId());
                    collect.setProductName(c.getName());
                    collect.setProductImg(c.getProductImg());
                    collect.setProductPrice(c.getPrice());
                    User user = (User)req.getSession().getAttribute("user");
                    collect.setUserId(user.getId());
                     collectService.addCollect(collect);


                    Result result = new Result("200", "添加成功", null);

                    out.print(JSON.toJSONString(result));
                }
                break;
            case "/del":
                resp.setContentType("application/json;charset=utf-8");

                PrintWriter outd = resp.getWriter();
                String id = req.getParameter("id");
                int count = 0;
                if(id!=null&&id.trim().length()>0)
                {
                    count = collectService.delById(Integer.valueOf(id));
                }
                Result result1;
                if(count!=0)
                {
                    result1 = new Result("200","删除成功",null);
                }
                else
                {
                    result1 = new Result("400","删除失败",null);
                }
                outd.print(JSON.toJSONString(result1));
                 break;

            case "/content":

                User user = (User) req.getSession().getAttribute("user");
                String pageNow = req.getParameter("pageNow");
                if (pageNow == null) {
                    pageNow = "1";
                }
                PageInfo<Collect> pageInfo = collectService.findAll(user.getId(), Integer.valueOf(pageNow), 3);
                req.setAttribute("pageInfo", pageInfo);
                req.getRequestDispatcher("/WEB-INF/collect/content.jsp").forward(req, resp);
                break;

            case "/index":
                req.getRequestDispatcher("/WEB-INF/collect/index.jsp").forward(req, resp);
                break;

        }


    }
}
